<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('course.unit.point.homework.view') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div class="mr10">
            <el-button icon="Back" @click="router.push({name: 'module_education_course_unit_point_homework_list', query: {course_id: dataForm.course_id, unit_id: dataForm.unit_id, point_id: dataForm.point_id}})">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form ref="formRef" :model="dataForm" label-width="100">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('course.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-descriptions border>

                <!-- 第1行 -->
                <el-descriptions-item width="200" :label="i18n('organization.title')" label-align="right" :span="3">
                  {{ dataForm.organization_title }}
                </el-descriptions-item>

                <!-- 第2行 -->
                <el-descriptions-item width="200" :label="i18n('course.title')" label-align="right">
                  {{ dataForm.course_title }}
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('course.unit.point.title')" label-align="right">
                  {{ dataForm.point_title }}
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('common.create_time')" label-align="right">
                  {{ dataForm.create_time }}
                </el-descriptions-item>

                <!-- 第3行 -->
                <el-descriptions-item width="200" :label="i18n('course.unit.point.homework.title')" label-align="right" :span="3">
                  {{ dataForm.title }}
                </el-descriptions-item>

                <!-- 第4行 -->
                <el-descriptions-item width="200" :label="i18n('course.unit.point.homework.content')" label-align="right" :span="3">
                  {{ dataForm.content }}
                </el-descriptions-item>
              </el-descriptions>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('course.unit.point.homework.resource_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row :gutter="20" class="mb20 text-center">
                <el-col :span="3" v-for="(item, key) in dataForm.resource" :key="key">
                  <el-card shadow="hover">
                    <div>
                      <el-image :src="item.url">
                        <template #error>
                          <div class="image-slot">
                            <el-icon :size="45"><Tickets /></el-icon>
                          </div>
                        </template>
                      </el-image>
                    </div>

                    <el-divider></el-divider>

                    <div>
                      <el-link :href="item.url" :underline="false" target="_blank">{{ item.filename }}</el-link>
                    </div>
                  </el-card>
                </el-col>
              </el-row>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="course-unit-point-homework-view">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('education/course/unit/point/homework')

  const dataForm = reactive({
    id: 0,
    organization_id: 0,
    course_id: 0,
    unit_id: 0,
    point_id: 0,
    organization_title: '',
    course_title: '',
    point_title: '',
    title: '',
    content: '',
    create_time: '',
    resource: []
  })

  const { formRef } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.organization_id = data.data.organization_id
            dataForm.title = data.data.title
            dataForm.content = data.data.content
            dataForm.create_time = data.data.create_time
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/organization/data`),
              method: 'get',
              params: $http.adornParams({
                'id': dataForm.organization_id
              }),
            }).then(({ data }) => {
              if (data && data.data && data.status === 200) {
                dataForm.organization_title = data.data.title
              }
            })
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/education/course/data`),
              method: 'get',
              params: $http.adornParams({
                'id': dataForm.course_id
              }),
            }).then(({ data }) => {
              if (data && data.data && data.status === 200) {
                dataForm.course_title = data.data.title
              }
            })
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/education/course/unit/point/data`),
              method: 'get',
              params: $http.adornParams({
                'course_id': dataForm.course_id,
                'unit_id': dataForm.unit_id,
                'id': dataForm.point_id
              }),
            }).then(({ data }) => {
              if (data && data.data && data.status === 200) {
                dataForm.point_title = data.data.title
              }
            })
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/resource/select`),
              method: 'get',
              params: $http.adornParams({
                'organization_id': dataForm.organization_id,
                'homework_id': dataForm.id,
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                dataForm.resource = data.data
              }
            })
          }
        })
      }
    })
  }

  onMounted(() => {
    dataForm.course_id = query.course_id
    dataForm.unit_id = query.unit_id
    dataForm.point_id = query.point_id

    init(formRef)
  })
</script>

<style lang="scss" scoped>
  .el-image {
    padding: 0 5px;
    max-width: 260px;
    max-height: 260px;
    width: 100%;
    height: 260px;
  }
</style>
